import Vue from 'vue'
import VueRouter from 'vue-router'
//引入仓库
import store from '../store/index'

//封装一个函数  哪里要使用就放到哪里激活
//haseUrl 用于判断是否含有该权限  如果含有next 没有就返回
//  url  路由地址   next  next()
function haseUrl(url, next) {
  let result = store.getters.getUser.menus_url.some(item => item == url)
  if (result) {
    next()
  } else {
    //没有权限就强制跳转到首页 
    next('/home')
  }
}

Vue.use(VueRouter)

export const routesList = [
  {
    path: "/menu",
    component: () => import("../views/menu/menu.vue"),
    meta: {
      title: "菜单管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/menu', next)
    }
  },
  {
    path: "/role",
    component: () => import("../views/role/role.vue"),
    meta: {
      title: "角色管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/role', next)
    }
  }, {
    path: "/admin",
    component: () => import("../views/admin/admin.vue"),
    meta: {
      title: "管理员管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/admin', next)
    }
  },
  {
    path: "/shop",
    component: () => import("../views/shop/shop.vue"),
    meta: {
      title: "商品分类"
    },
    beforeEnter(to, from, next) {
      haseUrl('/shop', next)
    }
  },
  {
    path: "/specs",
    component: () => import("../views/specs/specs.vue"),
    meta: {
      title: "商品规格"
    },
    beforeEnter(to, from, next) {
      haseUrl('/specs', next)
    }
  },
  {
    path: "/goods",
    component: () => import("../views/goods/goods.vue"),
    meta: {
      title: "商品管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/goods', next)
    }
  },
  {
    path: "/charts",
    component: () => import("../views/charts/charts.vue"),
    meta: {
      title: "轮播图管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/charts', next)
    }
  },
  {
    path: "/vips",
    component: () => import("../views/vvip/vvip.vue"),
    meta: {
      title: "会员管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/vips', next)
    }
  },
  {
    path: "/seckill",
    component: () => import("../views/seckill/seckill.vue"),
    meta: {
      title: "秒杀管理"
    },
    beforeEnter(to, from, next) {
      haseUrl('/seckill', next)
    }
  },
]

const routes = [
  {
    path: "/login",
    component: () => import("../pages/login.vue")
  }, {
    path: "/index",
    component: () => import("../pages/index.vue"),
    children: [
      {
        path: "/home",
        component: () => import("../views/home.vue"),
        meta: {
          title: "首页"
        }
      },
      ...routesList,
      {
        //二级路由重定向
        path: "",
        redirect: "/home"
      }
    ]
  }, {
    //一级路由重定向
    path: "*",
    redirect: "/index"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//调用全局前置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next()
    return
  }
  //如果已经登录 直接放行
  if (store.getters.getUser) {
    next()
    return
  }
  //以上条件都不符合  就直接强制跳转到登录
  next('/login')
})

export default router
